// Get a single parcel's info at an optional lon,lat.
function initParcelInfoStore() {
  return new Ext.data.JsonStore({
     url           : 'getParcelInfo.php'
    ,root          : 'data'
    ,totalProperty : 'count'
    ,idProperty    : 'ParcelNum'
    ,fields        : [
       'Parcel Num'
      ,'Regulated'
      ,'Owner Name'
      ,'Dep2HWT (in)'
      ,'Dep2ResLyr (in)'
      ,'Hyd Soil Group'
      ,'KSat (in/hr)'
      ,'Imperv Area (ac)'
      ,'UpLand Area (ac)'
      ,'Wetland Area (ac)'
      ,'Total Area (ac)'
      ,'Perv Load (lb/yr)'
      ,'Imperv Load'
      ,'Total Load (lb/yr)'
      ,'Slope (%)'
      ,'posList'
      ,'Load Credit/Deficit (lb/yr)'
    ]
    ,baseParams : {
      point : ''
    }
  });
}

// Pull back a list of parcels based on a bbox.
function initParcelsOnMapStore() {
  return new Ext.data.JsonStore({
     url           : 'getParcelsByBbox.php'
    ,root          : 'data'
    ,totalProperty : 'count'
    ,idProperty    : 'ParcelNum'
    ,remoteSort    : true
    ,fields        : [
       'Parcel Num'
      ,'Regulated'
      ,'Owner Name'
      ,'Actual Removal (lb/yr)'
    ]
  });
}

// Get a parcle's credit/deficit info.
function initParcelsCreditStore() {
  return new Ext.data.JsonStore({
     url           : 'getParcelsCredit.php'
    ,root          : 'data'
    ,totalProperty : 'count'
    ,idProperty    : 'ParcelNum'
    ,remoteSort    : true
    ,fields        : [
       'Parcel Num'
      ,'Owner Name'
      ,'Credit Removal (lb/yr)'
      ,'Town'
      ,'Load Credit/Deficit (lb/yr)'
      ,'Price ($)'
      ,'Price ($/lb/yr)'
      ,'Mail Addr1'
      ,'Mail City'
      ,'Mail State'
      ,'Mail Zip'
    ]
  });
}

// Init the goals.  This came straight from the original .xls.
function initGoalsStore() {
  return new Ext.data.ArrayStore({
     fields : ['name','bmpStoreName','weight','showOnIntroPage']
    ,data   : [
       ['Aesthetic'            ,'goalAesthetic',3,'yes']
      ,['Bacteria Reduction'   ,'goalBacteriaReduction',1,'no']
      ,['Cost'                 ,'COST',1,'yes']
      ,['Nitrogen Reduction'   ,'goalNitrogenReduction',1,'no']
      ,['Peak Flow Reduction'  ,'goalPeakFlowReduction',1,'no']
      ,['Phosophorus Reduction','goalPhosphorusReduction',1,'yes']
      ,['TSS/Metals Reduction' ,'goalTssMetalsReduction',1,'no']
      ,['Volume Reduction'     ,'goalVolumeReduction',1,'yes']
    ]
  });
}

// Init the BMP's.  This came straight from the original .xls.
function initBmpTypeStore() {
  return new Ext.data.ArrayStore({
     fields : [
       'short'
      ,'full'
      ,'fullBr'
      ,'extraBr'
      ,'unitCost'
      ,'min Dep2ResLyr (in)'
      ,'min Dep2HWT (in)'
      ,'Hyd Soil Group'
      ,'max Slope (%)'
      ,'min Total Area (ac)'
      ,'max Total Area (ac)'
      ,'suggestedConstraints'
      ,'goalAesthetic'
      ,'goalTssMetalsReduction'
      ,'goalPhosphorusReduction'
      ,'goalNitrogenReduction'
      ,'goalBacteriaReduction'
      ,'goalPeakFlowReduction'
      ,'goalVolumeReduction'
      ,'suggestedGoals'
      ,'overallScore'
      ,'wqd65'
      ,'detailsAbbrev'
      ,'details'
    ]  
    ,data  : [
       ['Bioret'  ,'Bioretention'                ,'Bioretention<br>'                   ,'<br>&nbsp;',10,1,1,'A/B/C/D',10,0 ,10    ,0,3,3,1,2,2,1,2,0,0,0.647058725357055,'A system that collects and filters stormwater through layers of mulch, soil, and roots, before returning the filtered water to the groundwater. Only excess water will enter the stormwater network.','Bioretention systems collect and filter stormwater through layers of mulch, soil, and root systems where pollutants such as bacteria, nitrogen, phosphorus, heavy metals, oil and grease are retained, degraded, and absorbed.  Some treated stormwater is infiltrated into the ground as groundwater and the excess is discharged via an underdrain system.  Bioretention systems can be planted with a variety of perennials, grasses, shrubs and small trees (natives are preferred).  A bioretention system without an underdrain is considered by the Exchange to be a rain garden.']
      ,['Greenst' ,'Tree Filter'                 ,'Tree Filter<br>'                    ,'<br>&nbsp;',15,1,1,'A/B/C/D',10,0 ,10    ,0,3,3,1,2,2,1,2,0,0,0.647058725357055,'An underground structure and above ground plantings which collect and treat stormwater using bioretention (see Bioretention). Treated water is returned to the groundwater and only excess water will enter the stormwater network.','Tree filters or tree pits consist of an underground structure and above ground plantings which collect and treat stormwater using bioretention (see Bioretention).  Numerous prefabricated tree pit structures are commercially available. These typically include a ready-made concrete box containing an appropriate soil mixture and may also include plantings, usually one tree or a few small shrubs.  Although underground they differ, above-ground tree filters closely resemble traditional street trees and work well for urban streets where space is limited.  The Exchange considers the tree filter to have the same removal efficiencies as a bioretention system.']
      ,['Gwetland','Gravel Wetland'              ,'Gravel Wetland<br>'                 ,'<br>&nbsp;',8 ,2,0,'A/B/C/D',25,0 ,999999,0,2,3,2,2,3,2,1,0,0,1.58333206176757,'Specially designed wetlands that filter stormwater as it flows through a series of gravel-filled compartments, each covered by vegetation and soils, that are designed to hold water at all times. Treated water is returned to the groundwater and only excess water will enter the stormwater network.','Subsurface gravel wetlands filter stormwater as it flows horizontally through a series of gravel wetland cells.  The wetland cells consist of a thin layer of wetland soil which supports a thick vegetative cover below which is a thick layer of gravel where algae and microbes grow in abundance.  Treatment occurs through physical, biological, and chemical reactions in the wetland soil and gravel layers.  Water is discharged through a submerged pipe in the final cell.  These systems are designed to maintain constant saturation of the wetland soils.']
      ,['Raingdn' ,'Rain Garden'                 ,'Rain Garden<br>'                    ,'<br>&nbsp;',5 ,2,3,'A/B'    ,15,0 ,10    ,0,3,3,2,2,3,1,2,0,0,0.232055723667145,'A shallow surface depression, planted with vegetation, that stores stormwater before it is filtered by layers of mulch, soil, and roots. Treated water is returned to the groundwater.','An infiltration practice that stores the water quality volume in a very shallow surface depression before it is infiltrated into the underlying soils.  Most of the treated stormwater is infiltrated into the ground as groundwater and only the excess overflows.  Rain gardens filter stormwater through layers of mulch, soil, and root systems, where pollutants such as bacteria, nitrogen, phosphorus, heavy metals, oil and grease are retained, degraded and absorbed.  Rain gardens can be planted with a variety of perennials, grasses, shrubs and small trees (natives preferred).  A rain garden with an underdrain is considered by the Exchange to be a bioretention system.']
      ,['Ibasin'  ,'Infiltration Basin'          ,'Infiltration Basin<br>'             ,'<br>&nbsp;',4 ,4,3,'A/B'    ,15,0 ,999999,0,1,3,3,2,3,2,3,0,0,0.232055723667145,'A stormwater basin with a very porous bottom, which will filter stormwater before returning it to the groundwater. Only excess water will enter the stormwater network.','An infiltration practice that stores the water quality volume in a shallow surface depression before it is infiltrated into the underlying soils.  Most of the treated stormwater is infiltrated into the ground as groundwater and only the excess overflows.']
      ,['Itrench' ,'Infiltration Trench'         ,'Infiltration Trench<br>'            ,'<br>&nbsp;',8 ,4,3,'A/B'    ,15,0 ,999999,0,3,3,3,2,3,2,3,0,0,0.3401,'A trench, usually filled with washed gravel, that captures stormwater and allows it to absorb into underlying soils. Only excess water will enter the stormwater network.','An infiltration practice that stores the water quality volume in a trench filled with clean gravel before it is infiltrated into underlying soils.  Most of the treated stormwater is infiltrated into the ground as groundwater and only the excess overflows.']
      ,['Ichamber','Subsurface Chamber'          ,'Subsurface<br>Chamber<br>'          ,''          ,12,4,3,'A/B'    ,15,0 ,999999,0,3,3,3,2,3,2,3,0,0,0.232055723667145,'One or more chambers installed below ground, built using a variety of manufactured products, that store stormwater runoff before it is absorbed into underlying soils. Only excess water will enter the stormwater network.','An infiltration practice that stores the water quality volume in a subsurface chamber sometimes filled with clean gravel before it is infiltrated into underlying soils.  Most of the treated stormwater is infiltrated into the ground as groundwater and only the excess overflows.']
      ,['Drypond' ,'Dry Extended Detention Basin','Dry Extended<br>Detention Basin<br>',''          ,2 ,2,1,'A/B/C/D',25,0 ,999999,0,1,2,1,1,1,3,2,0,0,100.000,'A pond designed to hold stormwater just long enough to reduce burden on the stormwater network. These systems are better at controlling water quantity than water quality.','A stormwater basin designed to capture, temporarily hold, and gradually release a volume of stormwater runoff to attenuate and delay stormwater runoff peaks.  Dry extended detention ponds provide water quantity control (peak flow control and stream channel protection) as opposed to water quality control.  These systems are known as "dry ponds" or "detention basins".']
      ,['Wetpond' ,'Wet Extended Detention Basin','Wet Extended<br>Detention Basin<br>',''          ,3 ,2,0,'A/B/C/D',25,10,999999,0,2,3,2,1,2,3,1,0,0,10.000,'A pond designed to hold a fixed volume of stormwater. Microorganisms in the water will break down contaminants. Treated water is returned to the groundwater and only excess water will enter the stormwater network.','A stormwater basin designed to store and treat stormwater runoff.  Unlike dry ponds, these ponds hold a fixed volume of water even during dry weather.  Stormwater sediment held in the pond settles to the bottom and nutrients are taken up by pond vegetation.  Over the long-term, microorganisms breakdown petroleum hydrocarbons and pond vegetation takes up metals in the sediment.  When stormwater enters the pond, it displaces a portion of the existing water, which then discharges from the system.  These systems are typically lined or sited in areas with impermeable soils and therefore do not infiltrate stormwater into the ground as groundwater.  These systems are known as "wet ponds" or "retention basins".']
      ,['Vegswale','Vegetated Swale'             ,'Vegetated Swale<br>'                ,'<br>&nbsp;',8 ,2,2,'A/B/C/D',5 ,0 ,999999,0,2,2,1,1,1,1,2,0,0,7.49999856948852,'A shallow, vegetated channel that slows down stormwater, and filters contaminants. Swales may add treated water to the stormwater network, or return it to the groundwater.','A shallow, vegetated channel that conveys and treats stormwater runoff by slowing runoff velocity, filtering out stormwater pollutants, reducing runoff temperatures and, under certain conditions, infiltrating runoff into the ground as groundwater.  Check dams can be placed periodically along the length of the swale slow runoff and promote filtration and infiltration.  Swales can be underlain with a layer of gravel to temporarily store runoff after it permeates through the soil layer and can also have an underdrain to convey the infiltrated water offsite.  Swales with specially designed soil mediums are considered to be water quality swales and not currently considered by the Exchange.']
    ]
  });
}

// Init the BMP constraints.  This came straight from the original .xls.
function initBmpConstraintStore() {
  return new Ext.data.ArrayStore({
     fields : [
       'short'
      ,'full'
      ,'costFactor'
    ]
    ,data  : [
       ['Outlet'        ,'Outlet modification'  ,0.1]
      ,['Underdeveloped','Underdeveloped area'  ,1.0]
      ,['Partdeveloped' ,'Partly developed area',1.5]
      ,['Developed'     ,'Developed area'       ,2.0]
      ,['Dryretrofit'   ,'Retrofit dry system'  ,2.0]
      ,['Wetretrofit'   ,'Retrofit wet system'  ,3.0]
    ]
  });
}

// Init the performance store.  This came straight from the original .xls.
// This is really just linear interpolation that could have been done w/i
// the JS itself.  But it was copied over from the .xls.
function initPerformanceStore() {
  return new Ext.data.ArrayStore({
     fields : [
       'WQD (in)'
      ,'Bioret'
      ,'Greenst'
      ,'Gwetland'
      ,'Raingdn'
      ,'Ibasin'
      ,'Itrench'
      ,'Ichamber'
      ,'Drypond'
      ,'Wetpond'
      ,'Vegswale'
    ]
    ,data  : [
       [0.000,0.0000000000,0.0000000000,0.0000000000,0.0000000000,0.0000000000,0.0000000000,0.0000000000,0.0000000000,0.0000000000,0.0000000000]
      ,[0.100,0.1980000000,0.1980000000,0.1960000000,0.4313333333,0.4313333333,0.2893333333,0.4313333333,0.0300000000,0.1607200000,0.0200000000]
      ,[0.200,0.3400000000,0.3400000000,0.2680000000,0.6193333333,0.6193333333,0.4840000000,0.6193333333,0.0600000000,0.2197600000,0.0500000000]
      ,[0.400,0.5280000000,0.5280000000,0.4160000000,0.8106666667,0.8106666667,0.7210000000,0.8106666667,0.0800000000,0.3411200000,0.0900000000]
      ,[0.600,0.6340000000,0.6340000000,0.5100000000,0.8923333333,0.8923333333,0.8416666667,0.8923333333,0.0900000000,0.4182000000,0.1300000000]
      ,[0.800,0.7020000000,0.7020000000,0.5720000000,0.9333333333,0.9333333333,0.9050000000,0.9333333333,0.1100000000,0.4690400000,0.1700000000]
      ,[1.000,0.7520000000,0.7520000000,0.6100000000,0.9573333333,0.9573333333,0.9426666667,0.9573333333,0.1200000000,0.5002000000,0.2100000000]
      ,[1.500,0.8320000000,0.8320000000,0.6480000000,0.9853333333,0.9853333333,0.9820000000,0.9853333333,0.1300000000,0.5313600000,0.2900000000]
      ,[2.000,0.8820000000,0.8820000000,0.6600000000,0.9936666667,0.9936666667,0.9926666667,0.9936666667,0.1400000000,0.5412000000,0.3600000000]
      ,[5.000,0.9500000000,0.9500000000,0.7000000000,1.0000000000,1.0000000000,1.0000000000,1.0000000000,0.1600000000,0.6000000000,0.6000000000]
      ,[10.000,1.0000000000,1.0000000000,0.7400000000,1.0000000000,1.0000000000,1.0000000000,1.0000000000,0.2000000000,0.6500000000,0.7000000000]
      ,[100.000,1.0000000000,1.0000000000,0.8000000000,1.0000000000,1.0000000000,1.0000000000,1.0000000000,0.6500000000,0.7000000000,0.7500000000]
    ]
  });
}

// Total impervious area.  Straight from .xls.
function calcTIA(parcelInfoStore) {
  var rec = parcelInfoStore.getAt(0);
  if (rec.get('Total Area (ac)') > 0) {
    return rec.get('Imperv Area (ac)') / rec.get('Total Area (ac)') * 100;
  }
}

// WQ area.  Straight from .xls.
function calcWQArea(parcelInfoStore) {
  var rec = parcelInfoStore.getAt(0);
  return (0.05 + 0.9 * calcTIA(parcelInfoStore) / 100) * rec.get('Total Area (ac)');
}

// WQ volume.  Straight from .xls.
function calcWQVolume(parcelInfoStore,wqDepth) {
  var rec = parcelInfoStore.getAt(0);
  return calcWQArea(parcelInfoStore) * 43560 * wqDepth / 12;
}

// WQ depth.  Straight from the .xls.  Here is the funky interpolation that could have been done
// differently and more straightforwardly in JS.
function calcWQD(stormwaterControlTypeShort,eff,performanceStore) {
  var idxRow1 = -1;
  performanceStore.each(function(rec) {
    if (eff > rec.get(stormwaterControlTypeShort)) {
      idxRow1++;
    }
  });
  if (idxRow1 < 0) {
    idxRow1 = 0;
  }
  var idxRow2 = idxRow1 + 1;

  if (idxRow2 > performanceStore.getCount() - 1) {
    return 100;
  }

  var x1 = performanceStore.getAt(idxRow1).get(stormwaterControlTypeShort);
  var x2 = performanceStore.getAt(idxRow2).get(stormwaterControlTypeShort);
  var y1 = performanceStore.getAt(idxRow1).get('WQD (in)');
  var y2 = performanceStore.getAt(idxRow2).get('WQD (in)');

  if (x2 - x1 == 0) {
    return 0;
  }
  else {
    return y1 + (y2 - y1) / (x2 - x1) * (eff - x1);
  }
}

// Efficiency.  Straight from the .xls.  Here is the funky interpolation that could have been done
// differently and more straightforwardly in JS.
function calcEff(stormwaterControlTypeShort,wqDepth,parcelInfoStore,performanceStore) {
  var idxRow1 = -1;
  performanceStore.each(function(rec) {
    if (wqDepth > rec.get('WQD (in)')) {
      idxRow1++;
    }
  });
  if (idxRow1 < 0) {
    idxRow1 = 0;
  }
  var idxRow2 = idxRow1 + 1;

  if (idxRow2 > performanceStore.getCount() - 1) {
    return 100;
  }

  var x1 = performanceStore.getAt(idxRow1).get('WQD (in)');
  var x2 = performanceStore.getAt(idxRow2).get('WQD (in)');
  var y1 = performanceStore.getAt(idxRow1).get(stormwaterControlTypeShort);
  var y2 = performanceStore.getAt(idxRow2).get(stormwaterControlTypeShort);

  if (x2 - x1 == 0) {
    return 0;
  }
  else {
    return y1 + (y2 - y1) / (x2 - x1) * (wqDepth - x1);
  }
}

function calcLoadRemoved(stormwaterControlTypeShort,wqDepth,parcelInfoStore,performanceStore) {
  return calcEff(stormwaterControlTypeShort,wqDepth,parcelInfoStore,performanceStore) * parcelInfoStore.getAt(0).get('Total Load (lb/yr)');
}

function getBmpConstraintCostFactor(stormwaterControlConstraintFull,stormwaterControlConstraintStore) {
  var idx = stormwaterControlConstraintStore.find('full',stormwaterControlConstraintFull);
  return stormwaterControlConstraintStore.getAt(idx).get('costFactor');
}

function getBmpTypeUnitCost(stormwaterControlTypeFull,stormwaterControlTypeStore) {
  var idx = stormwaterControlTypeStore.find('full',stormwaterControlTypeFull);
  return stormwaterControlTypeStore.getAt(idx).get('unitCost');
}

// not used
function calcUnitCost65(stormwaterControlTypeFull,stormwaterControlTypeStore) {
  var idx = stormwaterControlTypeStore.find('full',stormwaterControlTypeFull);
  return stormwaterControlTypeStore.getAt(idx).get('unitCost') * stormwaterControlTypeStore.getAt(idx).get('wqd65') / 12;
}

// not used
var townsPos = [
   new String('-71.3971103706069 42.1373283911779,-71.3820962782313 42.1355471453334,-71.3833351402105 42.1335139658082,-71.3801887217144 42.1329257829111,-71.3821712187291 42.1296459094898,-71.3677351972953 42.107303391881,-71.359273175385 42.0895197672863,-71.3653146000751 42.0761945753287,-71.3882352508191 42.0410044768,-71.455904646296 42.0348965447422,-71.4552741751423 42.0727534346981,-71.4537020160562 42.0726173108054,-71.4465907606018 42.1021375186468,-71.4416775313244 42.1318857216091,-71.4428855770668 42.1340762613193,-71.4401814114766 42.1352756342323,-71.4344907243222 42.1343538391187,-71.4347447959685 42.1355900290813,-71.4302780888948 42.1390094117799,-71.419248380794 42.1389712899967,-71.4205359259184 42.1375418571372,-71.4188457002615 42.1364024022562,-71.4150988411459 42.1382598319764,-71.4158331946602 42.1356346898915,-71.412957868748 42.133587741817,-71.4087662871525 42.1342291085133,-71.4024557457349 42.139539021588,-71.3971103706069 42.1373283911779').split(',')
  ,new String('-71.4414260151995 42.1348289097328,-71.4428855770668 42.1340762613193,-71.4416775313244 42.1318857216091,-71.4465907606018 42.1021375186468,-71.4537020160562 42.0726173108054,-71.4552741751423 42.0727534346981,-71.4547544298996 42.0440826905048,-71.4581161587184 42.0178523237059,-71.4982566732496 42.0172722425754,-71.4982401336723 42.0914124507856,-71.4952822636843 42.0999578620751,-71.4967025098047 42.1022504895619,-71.4995116615004 42.1030487605451,-71.5007202141462 42.1077351981087,-71.5029094380148 42.1094281254786,-71.5003835174587 42.1126952424353,-71.5014983401309 42.116588844442,-71.4785224777737 42.1313778974566,-71.442693163908 42.1357849665353,-71.4414260151995 42.1348289097328').split(',')
];

// not used
var basinsPos = [
   new String('-71.428267818845 42.1380038267869,-71.4237908199815 42.139084000051,-71.4248828276311 42.1362910564519,-71.428267818845 42.1380038267869').split(',')
  ,new String('-71.4282676383914 42.1381635303857,-71.432749144989 42.1326946749419,-71.4368620534612 42.1322468562829,-71.4414560594525 42.1346801513308,-71.4345721569086 42.1343267861014,-71.4306092182789 42.1388592599246,-71.4282676383914 42.1381635303857').split(',')
  ,new String('-71.4243985525094 42.1366508646891,-71.4223414233401 42.1372797108137,-71.4176265359355 42.1352056901037,-71.4205355363509 42.1304360626373,-71.4228341412578 42.1299874748058,-71.4182572995876 42.1150390105948,-71.414997612246 42.111165262238,-71.4164527218938 42.1080152110866,-71.4206862906417 42.1065777075089,-71.4188769706781 42.1032452524639,-71.4194897942434 42.0966733364173,-71.4063080302314 42.1001745672792,-71.4054648625893 42.0981031339204,-71.3979751064233 42.0947655139144,-71.3941178221094 42.0881896540783,-71.3994380949972 42.0869339969102,-71.4004140669941 42.0811727765687,-71.4081488905107 42.0811792427191,-71.4082731263245 42.0788384989695,-71.4126286328761 42.0753306392823,-71.4207229243408 42.077227212149,-71.4270106990237 42.0740802568254,-71.432323099917 42.0784951109169,-71.436071672142 42.0763364867176,-71.4369200173742 42.0739060805951,-71.4486417722321 42.0739119399504,-71.4521431641182 42.0779649173317,-71.4557691825629 42.0772461161412,-71.4567351793117 42.0784169067511,-71.4499580707729 42.0903884461837,-71.4498346373336 42.093629553122,-71.4538195008864 42.0993932893832,-71.4549033683622 42.105245808575,-71.4534490415498 42.110016925052,-71.4511517912579 42.109925936454,-71.4490929061257 42.1141565433855,-71.4510238678374 42.119019109151,-71.4427978786239 42.1221664130189,-71.4427926064745 42.1280184783831,-71.4397637507154 42.1335088622696,-71.432749144989 42.1326946749419,-71.4299651007995 42.1345836731096,-71.4309308084329 42.1363848928482,-71.4292359389605 42.137644302068,-71.4243985525094 42.1366508646891').split(',')
  ,new String('-71.4415779472688 42.1349503085783,-71.4481103970882 42.1327026603959,-71.4516204176568 42.1296431173612,-71.4573023535955 42.1336067709709,-71.4415779472688 42.1349503085783').split(',')
  ,new String('-71.4415779472688 42.1349503085783,-71.4397638361349 42.1334188306124,-71.4427926064745 42.1280184783831,-71.4427978786239 42.1221664130189,-71.4507816615508 42.1194691648034,-71.4530762628367 42.1236115930096,-71.4502931512125 42.1255911193821,-71.4516202118506 42.12991321251,-71.4415779472688 42.1349503085783').split(',')
  ,new String('-71.412306373621 42.1337140511314,-71.4129131915426 42.1322311583186,-71.4161787069911 42.1324136569783,-71.4181093287261 42.1360163213433,-71.4243986597232 42.1365608330887,-71.4237908199815 42.139084000051,-71.4190905663392 42.1389342657159,-71.4205359259184 42.1375418571372,-71.4188457002615 42.1364024022562,-71.4149816751989 42.1382228321368,-71.4158331946602 42.1356346898915,-71.412306373621 42.1337140511314').split(',')
  ,new String('-71.3872914488093 42.120684744577,-71.3821075958742 42.1119464236876,-71.3816301439681 42.1086147443139,-71.3842948938492 42.1060065355715,-71.3813980773912 42.1033926402458,-71.3847861847977 42.1017755192128,-71.3870907262337 42.0975463001873,-71.396527889696 42.0926934731222,-71.398458060641 42.0951260721791,-71.4054648625893 42.0981031339204,-71.4063080302314 42.1001745672792,-71.4194897942434 42.0966733364173,-71.4188769706781 42.1032452524639,-71.4206862906417 42.1065777075089,-71.4164527218938 42.1080152110866,-71.414997612246 42.111165262238,-71.4182572995876 42.1150390105948,-71.4228341412578 42.1299874748058,-71.4205355363509 42.1304360626373,-71.4181124641657 42.1335854676173,-71.4141228515039 42.1321420395024,-71.4120643949876 42.1337610495879,-71.4003337139408 42.1324009712746,-71.3987647557713 42.1302388333861,-71.3952570574478 42.1303257125381,-71.3872914488093 42.120684744577').split(',')
  ,new String('-71.3791802425927 42.1252900620681,-71.3800318008025 42.1226580753931,-71.3874123805906 42.1206848634796,-71.3952570574478 42.1303257125381,-71.3987647557713 42.1302388333861,-71.4003337139408 42.1324009712746,-71.412306373621 42.1337140511314,-71.4087662871525 42.1342291085133,-71.4024557457348 42.139539021588,-71.3971103706069 42.137328391178,-71.3820962782313 42.1355471453334,-71.3833351402105 42.1335139658083,-71.3801887217144 42.1329257829111,-71.3821712187291 42.1296459094898,-71.3791802425927 42.1252900620681').split(',')
  ,new String('-71.375554661528 42.1195922138595,-71.38294371363 42.1175292663612,-71.386448629169 42.1186131775284,-71.3872914488093 42.120684744577,-71.3800318008025 42.1226580753931,-71.3791802425927 42.1252900620681,-71.375554661528 42.1195922138595').split(',')
  ,new String('-71.4517413665869 42.1296431683631,-71.4505354544487 42.125051033399,-71.4530760632426 42.1238816884093,-71.4525932524434 42.122621043982,-71.4608182594691 42.1203733556324,-71.4591271918108 42.1173116805501,-71.4629985373752 42.1143419426443,-71.4688014494088 42.1168645639895,-71.4730340399976 42.116325446953,-71.4777467786546 42.1256897721016,-71.4825503795324 42.1290217796728,-71.4785224777737 42.1313778974565,-71.4573020121071 42.1341145349493,-71.4517413665869 42.1296431683631').split(',')
  ,new String('-71.4525932524434 42.122621043982,-71.4490929061257 42.1141565433855,-71.4511517912579 42.109925936454,-71.4534490415498 42.110016925052,-71.4549033683622 42.105245808575,-71.4538195008864 42.0993932893832,-71.4498346373336 42.093629553122,-71.4499580707729 42.0903884461837,-71.4544369375876 42.0812970510943,-71.456613407008 42.0797673458806,-71.460117719764 42.0804888802008,-71.4610827430827 42.0834602804556,-71.47111903765 42.0728394270849,-71.4717222803326 42.0750003568252,-71.4754677089349 42.0769819342893,-71.4736528517219 42.0821133766049,-71.4783659643059 42.0831947523646,-71.4798148033874 42.0878766974528,-71.4818692498152 42.0890474608078,-71.4815057961715 42.0919284348263,-71.4777578482111 42.0940885415735,-71.476910272358 42.0979597577627,-71.4720755983069 42.0959779731942,-71.4707447869925 42.0983184774504,-71.4741271572642 42.1045315074531,-71.4747295137512 42.1099335642755,-71.4782349265409 42.1129053393758,-71.4782339082936 42.1158763932986,-71.4738798014778 42.1180362491359,-71.4727922314113 42.1162353578612,-71.4688014494088 42.1168645639895,-71.4626357250145 42.1144318568336,-71.4591271918108 42.1173116805501,-71.4608182594691 42.1203733556324,-71.4525932524434 42.122621043982').split(',')
  ,new String('-71.4738798014778 42.1180362491359,-71.4782339082936 42.1158763932986,-71.4782349265409 42.1129053393758,-71.4741254701988 42.1086729812614,-71.4741271572642 42.1045315074531,-71.470744621139 42.0986786060266,-71.4713499643944 42.0966980514256,-71.476910272358 42.0979597577627,-71.4781202129025 42.0948989012136,-71.4831972427942 42.0950798230378,-71.4847678169805 42.0988613938635,-71.4946802368297 42.1058847869561,-71.4949216915291 42.1102963670897,-71.4929868492947 42.1125470644724,-71.4940748532273 42.1151580523887,-71.4964932506493 42.1156983435004,-71.4967348756918 42.119972291118,-71.4824625738422 42.1290217662804,-71.4782303282438 42.1263200874932,-71.4738798014778 42.1180362491359').split(',')
  ,new String('-71.4781202129025 42.0948989012136,-71.4815057961715 42.0919284348263,-71.4818692498152 42.0890474608078,-71.4798148033874 42.0878766974528,-71.4783659643059 42.0831947523646,-71.4736528517219 42.0821133766049,-71.4763142539703 42.0753615287152,-71.4821149963758 42.0748223871594,-71.4830808663293 42.0781537338707,-71.490815456929 42.0772542914532,-71.4917824953003 42.0753636839034,-71.4957701437074 42.0792352946495,-71.4982622516105 42.0772546464126,-71.4981269345442 42.0934148557668,-71.4952822636843 42.0999578620752,-71.4967025098047 42.1022504895619,-71.4995116615004 42.1030487605451,-71.5007202141462 42.1077351981087,-71.5029094380148 42.1094281254786,-71.5003835174587 42.1126952424353,-71.5014983401309 42.116588844442,-71.4966139474247 42.1199298461516,-71.4964932506493 42.1156983435004,-71.4940748532273 42.1151580523887,-71.4929868492947 42.1125470644724,-71.4949216915291 42.1102963670897,-71.4946802368297 42.1058847869561,-71.4847678169805 42.0988613938635,-71.4831972427942 42.0950798230378,-71.4781202129025 42.0948989012136').split(',')
  ,new String('-71.386448629169 42.1186131775284,-71.3820972332948 42.1175283983452,-71.375554661528 42.1195922138595,-71.365694725238 42.1030151745439,-71.3689492137166 42.1015784292154,-71.3759606185644 42.1018563336034,-71.3737909320716 42.0986128032329,-71.375364795061 42.0974441074987,-71.3736730860643 42.0970821249483,-71.3754929174491 42.093752923264,-71.3730783655535 42.0922197232836,-71.3763481932733 42.089072168497,-71.3722496026256 42.0835756926984,-71.3827626948292 42.0845772960677,-71.3850654444032 42.0810683658107,-71.3909915777985 42.078643284742,-71.3993212220708 42.0844129876845,-71.3995601011761 42.0862138450762,-71.3941178221094 42.0881896540783,-71.396527596421 42.0928735374357,-71.3919319028765 42.0942197771511,-71.3870907262337 42.0975463001873,-71.3847861847977 42.1017755192128,-71.3813985817096 42.1031225442447,-71.3842948938492 42.1060065355715,-71.3816301439681 42.1086147443139,-71.3821075958742 42.1119464236876,-71.386448629169 42.1186131775284').split(',')
  ,new String('-71.365694725238 42.1030151745439,-71.359273175385 42.0895197672863,-71.3653146000751 42.0761945753287,-71.3794354175283 42.054592985644,-71.393659055839 42.07342390814,-71.39075514777 42.0755819545979,-71.3896609575973 42.0793622699197,-71.3848234052571 42.0812481874979,-71.3827626948292 42.0845772960677,-71.3722496026256 42.0835756926984,-71.3763481932733 42.089072168497,-71.3730783655535 42.0922197232836,-71.3754929174491 42.093752923264,-71.3736730860643 42.0970821249483,-71.375364795061 42.0974441074987,-71.3737909320716 42.0986128032329,-71.3759606185644 42.1018563336034,-71.3689492137166 42.1015784292154,-71.365694725238 42.1030151745439').split(',')
  ,new String('-71.3993212220708 42.0844129876845,-71.3902664671401 42.0786425920769,-71.3897857147181 42.0771115780438,-71.3937814025329 42.0725236963083,-71.3864254402556 42.0639635372166,-71.3916339130865 42.0563158080634,-71.389827702819 42.0528928385264,-71.3930913475271 42.0517255074659,-71.3949084171464 42.0486660748544,-71.3949210660912 42.041013292218,-71.4480688625253 42.0356038132874,-71.4472225389281 42.0367277897856,-71.4498787464827 42.0377193461623,-71.4506010827154 42.0406907454942,-71.4415410316017 42.0423970998849,-71.4385143184195 42.0497782043428,-71.4408045147855 42.0550913434663,-71.4401981078577 42.0576119436325,-71.4426140200084 42.0579733111194,-71.4433367283658 42.0604045529387,-71.4407985615332 42.0614836599524,-71.4412799253887 42.0635546581482,-71.4394657773421 42.0654443976682,-71.4426000374746 42.0734589068927,-71.4366782419948 42.0739959796089,-71.436071672142 42.0763364867176,-71.432323099917 42.0784951109169,-71.4270106990237 42.0740802568254,-71.4207229243408 42.077227212149,-71.4126286328761 42.0753306392823,-71.4082731263245 42.0788384989695,-71.4081488905107 42.0811792427191,-71.4004140669941 42.0811727765687,-71.3993212220708 42.0844129876845').split(',')
];

// Mmodified the storwater store based on the user-specified goal ranking.
// Ultimately comes up w/ suggestedGoals and overallScore.
function applyGoal(goalsStore,parcelInfoStore,stormwaterStore,gridPanel) {
  // special case for sorting by cost
  var sortByCost = false;
  goalsStore.each(function(rec) {
    if (rec.get('bmpStoreName') == 'COST' && rec.get('weight') == 3) {
      sortByCost = true;
    }
  });
  if (sortByCost) {
    stormwaterStore.each(function(rec) {
      var wqd         = calcWQD(rec.get('short'),pollutionReductionPercent / 100,performanceStore);
      var loadRemoved = calcLoadRemoved(rec.get('short'),wqd,parcelInfoStore,performanceStore);
      var costFactor  = getBmpConstraintCostFactor('Partly developed area',stormwaterControlConstraintStore);
      var unitCost    = getBmpTypeUnitCost(rec.get('full'),stormwaterGridPanel.getStore());
      var wqVolume    = calcWQVolume(parcelInfoStore,wqd);
      var totalCost   = costFactor * unitCost * wqVolume;
      rec.set('overallScore',-totalCost);
      rec.commit();
    });
  }
  else {
    stormwaterStore.each(function(rec) {
      var ckConstraints       = 0;
      var possibleConstraints = 0;
      if (rec.get('min Dep2ResLyr (in)') <= parcelInfoStore.getAt(0).get('Dep2ResLyr (in)')) {
        ckConstraints++;
      }
      possibleConstraints++;
      if (rec.get('min Dep2HWT (in)') <= parcelInfoStore.getAt(0).get('Dep2HWT (in)')) {
        ckConstraints++;
      }
      possibleConstraints++;
      if (arrayIntersects(rec.get('Hyd Soil Group').split('/'),parcelInfoStore.getAt(0).get('Hyd Soil Group').split('/'))) {
        ckConstraints++;
      }
      possibleConstraints++;
      if (parcelInfoStore.getAt(0).get('Slope (%)') <= rec.get('max Slope (%)')) {
        ckConstraints++;
      }
      possibleConstraints++;
      if (rec.get('min Total Area (ac)') <= parcelInfoStore.getAt(0).get('Total Area (ac)')) {
        ckConstraints++;
      }
      possibleConstraints++;
      if (parcelInfoStore.getAt(0).get('Total Area (ac)') <= rec.get('max Total Area (ac)')) {
        ckConstraints++;
      }
      possibleConstraints++;
      rec.set('suggestedConstraints',Math.round((ckConstraints / possibleConstraints) * 100));
      var ckGoals = 0;
      goalsStore.each(function(r) {
        if (r.get('weight') <= rec.get(r.get('bmpStoreName'))) {
          ckGoals++;
        }
      });
      rec.set('suggestedGoals',Math.round((ckGoals / goalsStore.getCount()) * 100));
      rec.set('overallScore'  ,Math.round((ckConstraints / possibleConstraints) * 50 + (ckGoals / goalsStore.getCount()) * 50));
      rec.commit();
    });
  }
  stormwaterStore.sort([{field : 'overallScore',direction : 'DESC'},{field : 'short',direction : 'ASC'}]);
  if (gridPanel) {
    gridPanel.getSelectionModel().selectRow(0);
    gridPanel.getView().focusRow(0);
  }
}
